<?php
namespace App\Http\Controllers\Admin;

use App\Models\GoodsType;
use Dingo\Api\Auth\Auth;
use Illuminate\Http\Request;
use App\Models\Orders;
use App\Exports\dumpExcel;
use Illuminate\Support\Facades\DB;
use App\Models\AdminUser;
use App\Events\WeChatNoticeEvent;
use Illuminate\Support\Facades\Log;

class OrderController extends Controller
{
    protected $model;
    protected $goodsType;
    public function __construct(Orders $order,GoodsType $goodsType, AdminUser $adminUser)
    {
        $this->model = $order;
        $this->goodsType = $goodsType;
        $this->adminUser = $adminUser;
    }
    public function index(Request $request)
    {
        $merchant = auth('admin')->user(); // 商户ID
        $merchantId = $merchant->id;
        $where = [];
        if($merchant->type == 4 ){
            $where[] = ['merchant_orders.operation_id','=',$merchantId];
        }
        $whereInStr = '';
        $whereInArr = [];
        if($merchant->type == 3 ){
            $ywusers =  DB::table('admin_users')->select('id')->where('parent_id',$merchantId)->get();
            $ywids = [];
            if($ywusers){
                foreach ($ywusers as $k => $v){
                    $ywids[] = $v->id;
                }
            }
            array_push($ywids,$merchantId);
            $whereInStr = 'merchant_orders.merchant_id';
            $whereInArr = $ywids;
        }
        if($merchant->type == 2){
           $users =  DB::table('admin_users')->select('id')->where('parent_id',$merchantId)->get();
           $shids = [];
           if($users){
               foreach ($users as $k => $v){
                   $shids[] = $v->id;
               }
           }
           array_push($shids,$merchantId);
           $whereInStr = 'merchant_orders.merchant_id';
           $whereInArr = $shids;   
        }    
        if(1 == $merchant->type){
            $result = $this->model
                ->leftJoin('order_total','order_total.ordertotalid','=','orders.ordersnp')
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->select('orders.id','goods.goods_name as goodsname','goods.goods_image','goods.domainName','orders.ordersn as order_sn', 'orders.ordersnp as order_snp','orders.machines_imel as machines_imel','orders.machines_name','orders.goods_total_price as goods_total_price','orders.order_total_price','orders.payment',
                    'orders.order_status','orders.refund_status','orders.payment_time','orders.created_at','goods.category_id','orders.freightway_id','orders.ordersn','orders.fault','order_total.ordertotalid','order_total.created_at as zcreated_at', 'admin_users.name','admin_users.name as adminname'
                )
                //->where('orders.ordersnp','!=',NULL)
                ->filter($request->all())
                ->where('orders.order_status','!=','0')
                ->where($where)
                ->orderBy('orders.created_at','desc')
                ->paginate(config('api.page.limit'));
            //计算总金额 
            $totalPrice = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereNotIn('orders.order_status',[0])
                ->sum('orders.order_total_price');
            //已收款合计总金额   
            $totalPricea = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereNotIn('orders.order_status',[0])
                ->sum('orders.order_total_price');
            //已退款合计总金额   
            $totalPriceb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',2)
                ->sum('orders.order_total_price');
            //未退款合计总金额   
            $totalPricec = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->sum('orders.order_total_price');
             //总订单数
             $totalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where($where)
                ->count();
            //微信总订单数
            $wxtotalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.payment', 1)
                ->where($where)
                ->count();
            //支付宝总订单数
            $zfbtotalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.payment', 2)
                ->where($where)
                ->count();
            //统计微信已收款金额
            $wxrefStatusa = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->where('orders.payment', 1)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计微信退款成功金额
            $wxrefStatusb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[0])
                ->where('orders.payment', 1)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计微信未退款金额        
            $wxrefStatusc = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->where('orders.payment', 1)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计支付宝已收款金额
            $zfrefStatusa = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->where('orders.payment', 2)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计支付宝退款成功金额
            $zfrefStatusb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',2)
                ->where('orders.payment', 2)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计支付宝未退款金额        
            $zfrefStatusc = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->where('orders.payment', 2)
                ->where($where)
                ->sum('orders.order_total_price');
            
        }else if($merchant->type != 4 && 1 != $merchant->type){
            $result = $this->model
                ->leftJoin('order_total','order_total.ordertotalid','=','orders.ordersnp')
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->select('orders.id','goods.goods_name as goodsname','goods.goods_image','goods.domainName','orders.ordersn as order_sn', 'orders.ordersnp as order_snp','orders.machines_imel as machines_imel','orders.machines_name','orders.goods_total_price as goods_total_price','orders.order_total_price','orders.payment',
                    'orders.order_status','orders.refund_status','orders.payment_time','orders.created_at','goods.category_id','orders.freightway_id','orders.ordersn','orders.fault','order_total.ordertotalid','order_total.created_at as zcreated_at', 'admin_users.name','admin_users.name as adminname'
                )
                ->filter($request->all())
                ->where('orders.order_status','!=','0')
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->orderBy('orders.created_at','desc')
                ->paginate(config('api.page.limit'));
            //计算总金额 
            $totalPrice = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereNotIn('orders.order_status',[0])
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            //已收款合计总金额   
            $totalPricea = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->whereNotIn('orders.order_status',[0])
                ->sum('orders.order_total_price');
            //已退款合计总金额   
            $totalPriceb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',2)
                ->sum('orders.order_total_price');
            //未退款合计总金额   
            $totalPricec = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->sum('orders.order_total_price');
             //总订单数
             $totalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->whereIn($whereInStr,$whereInArr)
                ->where($where)
                ->count();
            //微信总订单数
            $wxtotalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.payment', 1)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->count();

            //支付宝总订单数
            $zfbtotalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.payment', 2)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->count();
            //统计微信已收款金额
            $wxrefStatusa = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->where('orders.payment', 1)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            //统计微信退款成功金额
            $wxrefStatusb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[0])
                ->where('orders.payment', 1)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            //统计微信未退款金额        
            $wxrefStatusc = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->where('orders.payment', 1)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            //统计支付宝已收款金额
            $zfrefStatusa = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->where('orders.payment', 2)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            //统计支付宝退款成功金额
            $zfrefStatusb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',2)
                ->where('orders.payment', 2)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            //统计支付宝未退款金额        
            $zfrefStatusc = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->where('orders.payment', 2)
                ->where($where)
                ->whereIn($whereInStr,$whereInArr)
                ->sum('orders.order_total_price');
            
        }else{
            $result = $this->model
                ->leftJoin('order_total','order_total.ordertotalid','=','orders.ordersnp')
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->select('orders.id','goods.goods_name as goodsname','goods.goods_image','goods.domainName','orders.ordersn as order_sn', 'orders.ordersnp as order_snp','orders.machines_imel as machines_imel','orders.machines_name','orders.goods_total_price as goods_total_price','orders.order_total_price','orders.payment',
                    'orders.order_status','orders.refund_status','orders.payment_time','orders.created_at','goods.category_id','orders.freightway_id','orders.ordersn','orders.fault','order_total.ordertotalid','order_total.created_at as zcreated_at', 'admin_users.name','admin_users.name as adminname'
                )
                ->filter($request->all())
                ->where('orders.order_status','!=','0')
                ->where($where)
                ->orderBy('orders.created_at','desc')
                ->paginate(config('api.page.limit'));
  
            //计算总金额 
            $totalPrice = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereNotIn('orders.order_status',[0])
                ->sum('orders.order_total_price');
            //已收款合计总金额   
            $totalPricea = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereNotIn('orders.order_status',[0])
                ->sum('orders.order_total_price');
            //已退款合计总金额   
            $totalPriceb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',2)
                ->sum('orders.order_total_price');
            //未退款合计总金额   
            $totalPricec = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->where($where)
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->sum('orders.order_total_price');
             //总订单数
             $totalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where($where)
                ->count();
            //微信总订单数
            $wxtotalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.payment', 1)
                ->where($where)
                ->count();
            //支付宝总订单数
            $zfbtotalPrices = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.payment', 2)
                ->where($where)
                ->count();
            //统计微信已收款金额
            $wxrefStatusa = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->where('orders.payment', 1)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计微信退款成功金额
            $wxrefStatusb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->whereNotIn('orders.refund_status',[0])
                ->where('orders.payment', 1)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计微信未退款金额        
            $wxrefStatusc = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->where('orders.payment', 1)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计支付宝已收款金额
            $zfrefStatusa = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereNotIn('orders.order_status',[0])
                ->where('orders.payment', 2)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计支付宝退款成功金额
            $zfrefStatusb = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',2)
                ->where('orders.payment', 2)
                ->where($where)
                ->sum('orders.order_total_price');
            //统计支付宝未退款金额        
            $zfrefStatusc = $this->model
                ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
                ->leftJoin('admin_users','admin_users.id','=','merchant_orders.merchant_id')
                ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
                ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
                ->filter($request->all())
                ->whereIn('orders.order_status', [1,2,3,4])
                ->where('orders.refund_status',0)
                ->where('orders.payment', 2)
                ->where($where)
                ->sum('orders.order_total_price');
            
        }
           if($result){
            foreach ($result as $k => $v){
                if(1 == $v->payment){
                    $result[$k]->payment = '微信';
                }else if(2 == $v->payment){
                    $result[$k]->payment = '支付宝';
                }else{
                    $result[$k]->payment = '-';
                }
            }
        }
        $cateArr = $this->goodsType->get();
        $cates = [];
        foreach($cateArr as $k => $v){
            $cates[$v->id] = $v->goods_name;  
        }
        $admin_user = $this->adminUser->select('id', 'name')->where('type',3)->where('parent_id',$merchantId)->get();
        $adminUsers = [];
        foreach($admin_user as $k => $v){
            $adminUsers[$v->id] = $v->name;  
        }
        return view('admins.order.index')->with('result',$result)
            ->with('totalPrice',$totalPrice)->with('categs',$cates)
            ->with('wxrefStatusa',$wxrefStatusa)->with('wxrefStatusb',$wxrefStatusb)->with('wxrefStatusc',$wxrefStatusc)
            ->with('zfrefStatusa',$zfrefStatusa)->with('zfrefStatusb',$zfrefStatusb)->with('zfrefStatusc',$zfrefStatusc)
            ->with('totalPricea',$totalPricea)->with('totalPriceb',$totalPriceb)->with('totalPricec',$totalPricec)
            ->with('adminUsers',$adminUsers)->with('totalPrices',$totalPrices)->with('wxtotalPrices',$wxtotalPrices)
            ->with('zfbtotalPrices',$zfbtotalPrices);
    }
    //导出订单列表
    public function putOrderExcel(Request $request,dumpExcel $dumpExcel)
    {
        $req_params = $request->all();
    	return $dumpExcel->withinDays($req_params, date("Y-m-d"));
    }
    //手动退款
    public function refoundOrder(Request $request){
        $orderid = $request->id;
        $orders = $this->model->find($orderid);
            if(in_array(optional($orders)->order_status,[3,4]) && optional($orders)->refund_status == 0){
                // 判断订单是否自动退款还是手动退款
            if(in_array(optional($orders)->freight_status,[3,4,5,6])){
                $machine = DB::table('machines')->where('imei',$orders->machines_imel)->where('is_auto_refund',0)->first(); 
            }else{
                $machine = DB::table('machines')->where('imei',$orders->machines_imel)->first();
            }
            if($machine){
                if($orders){
                    $refundOrderSn = $this->refundOrderSn();
                    $data = array(
                        'goodsid' => $orders->goodsid,
                        'userid' => $orders->user_id,
                        'ordersn' => $orders->ordersn,  
                        'refund_ordersn' => $refundOrderSn,
                        'return_num' => 1,
                        'created_at' => date('Y-m-d H:i:s', time()),
                        'updated_at' => date('Y-m-d H:i:s', time()),
                        'return_content' => $orders->order_status == 3 ? '超时退款' : '设备故障退款'
                    );
                    $refundsId = DB::table('refunds')->insertGetId($data);
                    if($refundsId){
                        $merchantInfo = DB::table('merchant_orders')
                            ->leftJoin('users_codes', 'users_codes.merchant_id', '=', 'merchant_orders.merchant_id')
                            ->select('users_codes.customerNum', 'users_codes.shopNum', 'users_codes.shopAccess', 'users_codes.shopSecret')
                            ->where('order_id', $orders->id)
                            ->first();
                        $pay = new \App\Http\Controllers\Api\PayOrdercatrController;
                        //判断ordersn是否为主订单
                        $index_char = substr($orders->ordersn, 0, 1);
                        $ordersn = $index_char == 'Z' ? substr($orders->ordersn,0,-2) : $orders->ordersn;
                        $refundPartAmount = $orders->order_total_price;
                        Log::info('refundresult', [$ordersn,$refundOrderSn]);
                        $result = $pay->refundDLBPay($ordersn, $refundOrderSn, $merchantInfo, $refundPartAmount);
                        Log::info('refundresult', [$result]);
                        if('success' == $result['result']){
                            $manager = DB::table('manager_third_auths')
                                ->leftJoin('merchant_orders','merchant_orders.merchant_id','=','manager_third_auths.manager_id')
                                ->where('merchant_orders.order_id',$orders->id)
                                ->first();
                            if($manager){
                                if($manager->push_type){
                                    $push = json_decode($manager->push_type,1);
                                    $ispush = $push['lack']['val'];
                                    if($ispush){
                                        // 推送
                                        //退款通知订单中加入总订单号 总金额
                                        $msg = '退款通知 -- 单号：'.$ordersn.' 金额：'.$refundPartAmount;
                                        event(new WeChatNoticeEvent($manager->wx_open_id, $msg));
                                    }
                                }
                            }
                            $this->model->where('ordersn', $orders->ordersn)->update(['refund_status' => 2]);
                            return $this->success();
                        }
                        return $this->expirss();
                    }
                    
                } 
            }   
        } 
    }
    // 生成订单SN
    protected function refundOrderSn()
    {
        $orderSn = '';
        $orderSn = 'HRCTK_';
        $orderSn .= date('YmdHis');
        $orderSn .= mt_rand(1, 99999);
        return $orderSn;
    }
}
